package com.snopy.order.controller;

import com.snopy.pojo.JSONResult;
import com.snopy.controller.BaseController;
import com.snopy.enums.OrderStatusEnum;
import com.snopy.enums.PayMethodEnum;
import com.snopy.order.pojo.OrderStatus;
import com.snopy.order.pojo.bo.SubmitOrderBO;
import com.snopy.order.pojo.vo.MerchantOrdersVO;
import com.snopy.order.pojo.vo.OrderVO;
import com.snopy.order.service.OrdersService;
import com.snopy.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * TODO
 *
 * @author snoopy
 * @date 2020/9/18 0018 23:14
 * @email 77912204@qq.com
 * @remark
 */
@Api(value = "订单相关",tags = "订单相关Api")
@RestController
@RequestMapping("orders")
public class OrderConroller extends BaseController {

    @Autowired
    private OrdersService ordersService;


    @ApiOperation(value = "用户下单", notes = "用户下单", httpMethod = "POST")
    @PostMapping("/create")
    public Object create(@RequestBody SubmitOrderBO submitOrderBO,
                         HttpServletRequest request,
                         HttpServletResponse response){
        if (!submitOrderBO.getPayMethod().equals(PayMethodEnum.WEIXIN.type)
                && !submitOrderBO.getPayMethod().equals(PayMethodEnum.ALIPAY.type)  ) {
            return JSONResult.errorMsg("支付方式不支持！");
        }

        // 1. 创建订单
        OrderVO orderVO = ordersService.createOrder(submitOrderBO);
        String orderId = orderVO.getOrderId();
        // 2. 创建订单以后，移除购物车中已结算（已提交）的商品
        /**
         * 1001
         * 2002 -> 用户购买
         * 3003 -> 用户购买
         * 4004
         */
        //CookieUtils.setCookie(request, response, FOODIE_SHOPCART, "", true);
        // 3. 向支付中心发送当前订单，用于保存支付中心的订单数据
        MerchantOrdersVO merchantOrdersVO = orderVO.getMerchantOrdersVO();
        merchantOrdersVO.setReturnUrl(payReturnUrl);
        /*构建请求头*/
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add("imoocUserId","imooc");
        httpHeaders.add("password","imooc");
        /*请求体*/
        HttpEntity<MerchantOrdersVO> entity = new HttpEntity<>(merchantOrdersVO,httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(paymentUrl, entity, Object.class);
        Object body = objectResponseEntity.getBody();
        JSONResult jsonResult = JsonUtils.jsonToPojo(JsonUtils.objectToJson(body), JSONResult.class);
        if (jsonResult!=null&&jsonResult.getStatus()!=200){
            return JSONResult.errorMsg("支付中心创建订单失败，请联系管理员！");
        }
        return JSONResult.ok(orderId);
    }
    @ApiOperation(value = "支付状态")
    @PostMapping("notifyMerchantOrderPaid")
    public Integer notifyMerchantOrderPaid(String merchantOrderId) {
        ordersService.updateOrderStatus(merchantOrderId, OrderStatusEnum.WAIT_DELIVER.type);
        return HttpStatus.OK.value();
    }
    @ApiOperation(value = "获取支付状态")
    @PostMapping("getPaidOrderInfo")
    public JSONResult getPaidOrderInfo(String orderId) {

        OrderStatus orderStatus = ordersService.queryOrderStatusInfo(orderId);
        return JSONResult.ok(orderStatus);
    }


}
